<?php

//this file is aiming for handle the add user, show user, displaying roster and create new roster option from admin account
require_once 'class/admin.php';

if (isset($_REQUEST["gName"])) {
    $gName = $_REQUEST["gName"];
}
if (isset($_REQUEST["sName"])) {
    $sName = $_REQUEST["sName"];
}
if (isset($_REQUEST["email"])) {
    $email = $_REQUEST["email"];
}
if (isset($_REQUEST["tip"])) {
    $tip = $_REQUEST["tip"];
}
if (isset($_REQUEST['startDate'])) {
    $startDate = $_REQUEST['startDate'];
}
if (isset($_REQUEST['endDate'])) {
    $endDate = $_REQUEST['endDate'];
}
session_start();
$admin = unserialize($_SESSION['admin']);
$response;
$adminTemp = new admin($admin->getEmail(), $admin->getPW());

if ($tip == 3) {
    $response = $adminTemp->addStaff($gName, $sName, $email);
} else if ($tip == 4) {
    $result = $adminTemp->fetchStaffArray();
    if (count($result) != 0) {
        $tempString = "";
        for ($i = 0; $i < count($result); $i++) {
            $StaffInitialString;
            if ($adminTemp->checkStaffInitial($result[$i]['user_id'])) {
                $StaffInitialString ="1";//initial already
            } else {
                $StaffInitialString = "0";//need initial
            }
            $tempString = $tempString . $result[$i]['gName'] . "|" . $result[$i]['surname'] . "|" . $result[$i]['email'] . "|" . $StaffInitialString . "<br>";
        }
        $response = $tempString;
    } else {
        $response = "no user under";
    }
} else if ($tip == 1) {
    $DutyArray = $adminTemp->fetchDutyArray();
    $SundayArray = $adminTemp->fetchSundayArray();
    $dutyTableHeadString = "<th>Date</th>";
    $SundayString = "";
    foreach ($DutyArray as $value) {
        $dutyTableHeadString = $dutyTableHeadString . "<th>" . $value["dutyName"] . "</th>";
    }
    $dutyTableHeadString = "<thead><tr>" . $dutyTableHeadString . "</tr></thead>";
    $emptyString = "";
    for ($i = 0; $i < count($DutyArray); $i++) {
        $emptyString = $emptyString . "<td></td>";
    }
    foreach ($SundayArray as $value) {
        $SundayString = $SundayString . "<tr><td>" . $value["Date"] . "</td>" . $emptyString . "</tr>";
    }
    $SundayString = "<tbody>" . $SundayString . "</tbody>";
    $response = "<table cellspacing='2'>" . $dutyTableHeadString . $SundayString . "</table>";
} else if ($tip == 2) {
    $response = $adminTemp->CreateRosterTable($startDate, $endDate);
} else if ($tip == 11) {
    $response = $adminTemp->genereateRoster();
}

echo $response;
?>